package com.controllor;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.dao.AdminDao;
import com.dao.HumanDao;
import com.dao.OrdinaryEmployeesDao;
import com.entity.Deptrole;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.util.JSON_to_layui;
import com.util.SelectDao;

/**
 * Servlet implementation class AdminServlet
 */
@WebServlet("/AdminServlet")
public class AdminServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AdminServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		HttpSession session = request.getSession();
		String button = request.getParameter("button");
		if("1".equals(button)) {
			System.out.println("Human..button"+button);
			int limit = Integer.parseInt(request.getParameter("limit"));
			int page = (Integer.parseInt(request.getParameter("page"))-1) * limit;
			ObjectMapper mapper = new ObjectMapper();
			mapper.enable(SerializationFeature.INDENT_OUTPUT);	//序列化
			String json = mapper.writeValueAsString(AdminDao.getDeptList(page,limit));
			if(json != null || json.length() != 0) {
				json = JSON_to_layui.getLayuiData(json,0,AdminDao.count,"");
			}
			response.getWriter().print(json);
		}
		//部门更名等
		else if("2".equals(button)) {
			String type = request.getParameter("type");
			int deptid = Integer.parseInt(request.getParameter("deptid"));
			int roleid = Integer.parseInt(request.getParameter("roleid"));
			String newvalue = request.getParameter("newvalue");
			System.out.println("type"+type+"deptid"+deptid+"newvalue"+newvalue);
			
			PrintWriter out = response.getWriter();
			if(AdminDao.updateDeptroleInfo(deptid,roleid, type, newvalue)) {
				out.println("修改成功！");
			}else {
				out.println("修改失败！");
			}
		}//删除部门信息
		else if("3".equals(button)) {
			PrintWriter out = response.getWriter();
			System.out.println("Human..button"+button);
			int deptid = Integer.parseInt(request.getParameter("deptid"));
			if(AdminDao.deleteDept(deptid)) {
				out.print("删除成功！");
			}else {
				out.print("删除失败！");
			}
		}
		//新建部门
		else if("4".equals(button)) {
			boolean action = true;
			System.out.println("servlet..button"+button);
			PrintWriter out = response.getWriter();
			//普通职员
			String deptname = request.getParameter("deptname");
			int roleid_1 = Integer.parseInt(request.getParameter("roleid_1"));
			String rolename_1 = request.getParameter("rolename_1");
			float basewages_1 = Float.parseFloat(request.getParameter("basewages_1"));
			float overtimestandard_1= Float.parseFloat(request.getParameter("overtimestandard_1"));
			float leavestandard_1 = Float.parseFloat(request.getParameter("leavestandard_1"));
			float absentstandard_1 = Float.parseFloat(request.getParameter("absentstandard_1"));
			System.out.println("获取数据:"+deptname+" "+roleid_1+" "+rolename_1+" "+basewages_1+" "+overtimestandard_1+" "+leavestandard_1+" "+absentstandard_1);
//			Deptrole deptrole_1 = new Deptrole(deptname,roleid_1,rolename_1,basewages_1,overtimestandard_1,leavestandard_1,absentstandard_1);
			if(AdminDao.deptroleadd(deptname,0,roleid_1,rolename_1,basewages_1,overtimestandard_1,leavestandard_1,absentstandard_1)) {
				action = true;
			}else {
				action = false;
			}
			//部门经理
			int roleid_2 = Integer.parseInt(request.getParameter("roleid_2"));
			String rolename_2 = request.getParameter("rolename_2");
			float basewages_2 = Float.parseFloat(request.getParameter("basewages_2"));
			float overtimestandard_2= Float.parseFloat(request.getParameter("overtimestandard_2"));
			float leavestandard_2 = Float.parseFloat(request.getParameter("leavestandard_2"));
			float absentstandard_2 = Float.parseFloat(request.getParameter("absentstandard_2"));
			int id = -1;
			if(1 == SelectDao.selectfromdeptname(deptname).get(0).getRoleid()) {
				id = SelectDao.selectfromdeptname(deptname).get(0).getDeptid();
			}else {
				id = SelectDao.selectfromdeptname(deptname).get(1).getDeptid();
			}
			if(AdminDao.deptroleadd(deptname,id,roleid_2,rolename_2,basewages_2,overtimestandard_2,leavestandard_2,absentstandard_2)) {
				action = true;
			}else {
				action = false;
			}
			if(action) {
				out.println("部门新建成功！");
			}else {
				out.println("部门新建出现异常！");
			}
	}
		//合并
		else if("5".equals(button)) {
			System.out.println("servlet..button"+button);
			PrintWriter out = response.getWriter();
			int deptid1 = Integer.parseInt(request.getParameter("deptid1"));
			int deptid2 = Integer.parseInt(request.getParameter("deptid2"));
			if(deptid1 == deptid2) {
				out.print("合并取消，应是两个不同的部门");
				return ;
			}
			String deptname = request.getParameter("deptname");
			//开始合并
			if(AdminDao.isExit(deptid1) && AdminDao.isExit(deptid2)) {
				AdminDao.updateDeptroleInfo(deptid1, 0, "deptname", deptname);
				AdminDao.deleteDept(deptid2);
				out.print("合并成功，且新部门名字为<span style='color:#FFB800'>"+deptname+"</span>");
			}else {
				out.print("合并失败，请检查被合并部门是否原本存在？");
			}
		}
}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
